using Microsoft.VisualBasic.CompilerServices;
using SJVars;
using SJXmlP.XMLClasses;
using System;
using System.Reflection;
using System.Text;

namespace yW5Lib
{
	public class clsChapter : clsBase
	{
		private clsCollection<int> SceneIDs;

		private ChapterTypes mvarType;

		private bool mvarSectionStart;

		private int mvarBookID;

		private int mvarPartID;

		public bool AddedToProject;

		public int ProgressiveCount;

		public const string ObjectName = "Chapter";

		public ChapterTypes Type
		{
			get
			{
				return mvarType;
			}
			set
			{
				mvarType = value;
			}
		}

		public int SceneCount => SceneIDs.Count();

		public bool SectionStart
		{
			get
			{
				return mvarSectionStart;
			}
			set
			{
				mvarSectionStart = value;
			}
		}

		public int BookID
		{
			get
			{
				return mvarBookID;
			}
			set
			{
				mvarBookID = value;
			}
		}

		public int PartID
		{
			get
			{
				return mvarPartID;
			}
			set
			{
				mvarPartID = value;
			}
		}

		public int get_SceneIDByIndex(int Index)
		{
			int num;
			try
			{
				num = this.SceneIDs[Index];
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				num = -1;
				ProjectData.ClearProjectError();
			}
			return num;
		}

		public clsChapter()
		{
			SceneIDs = new clsCollection<int>();
		}

		public bool ChangeSceneID(int OldID, int NewID)
		{
			bool result;
			if (SceneIDs.Contains(OldID))
			{
				SceneIDs.Add(NewID, Conversions.ToString(OldID));
				SceneIDs.Remove(Conversions.ToString(OldID));
				result = true;
			}
			else
			{
				result = false;
				clsShared.cLog.AddAction("Tried to change scene id from " + Conversions.ToString(OldID) + " to " + Conversions.ToString(NewID) + " in chapter " + Conversions.ToString(ID) + " but oldID unknown.", MethodBase.GetCurrentMethod());
			}
			return result;
		}

		public void AddSceneID(int ScID)
		{
			string key = Conversions.ToString(ScID);
			if (!SceneIDs.Contains(key))
			{
				SceneIDs.Add(ScID, key);
			}
		}

		public string ShortTitle()
		{
			return Title.Replace("Chapter", "Ch");
		}

		public void ClearScenes()
		{
			SceneIDs.Clear();
		}

		public void InsertSceneBeforeID(int ScID, int BeforeScID)
		{
			int beforeSceneIndex = SceneIndex(BeforeScID);
			InsertSceneIDBeforeIndex(ScID, beforeSceneIndex);
		}

		public void InsertSceneIDBeforeIndex(int ScID, int BeforeSceneIndex)
		{
			try
			{
				string text = Conversions.ToString(ScID);
				if (SceneIDs.Contains(text))
				{
					clsShared.cLog.AddAction("Tried to add sceneid " + text + " to chapter " + Conversions.ToString(ID) + " but it's already there!");
				}
				else if ((SceneCount == 0) | (BeforeSceneIndex == 0))
				{
					SceneIDs.Add(ScID, text);
				}
				else if (BeforeSceneIndex <= SceneCount)
				{
					SceneIDs.Add(ScID, text, BeforeSceneIndex);
				}
				else
				{
					SceneIDs.Add(ScID, text);
				}
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
			}
		}

		public void AppendSceneAfterID(ref int ScID, int AfterID)
		{
			int afterSceneIndex = SceneIndex(AfterID);
			AppendSceneIDAfterIndex(ref ScID, afterSceneIndex);
		}

		public void AppendSceneIDAfterIndex(ref int ScID, int AfterSceneIndex)
		{
			try
			{
				string key = Conversions.ToString(ScID);
				if ((SceneCount == 0) | (AfterSceneIndex == 0))
				{
					SceneIDs.Add(ScID, key);
				}
				else if (AfterSceneIndex <= SceneCount)
				{
					SceneIDs.Add(ScID, key, null, AfterSceneIndex);
				}
				else
				{
					SceneIDs.Add(ScID, key);
				}
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
			}
		}

		public void RemoveSceneByID(int ScID)
		{
			string key = Conversions.ToString(ScID);
			if (SceneIDs.Contains(key))
			{
				SceneIDs.Remove(key);
			}
		}

		public bool ContainsSceneID(int ScID)
		{
			string key = Conversions.ToString(ScID);
			return SceneIDs.Contains(key);
		}

		public void Clear()
		{
			BaseClear();
			mvarType = ChapterTypes.ChapterNormal;
			mvarSectionStart = false;
			SceneIDs.Clear();
		}

		public int SceneIndex(int ScID)
		{
			int result = -1;
			int sceneCount = SceneCount;
			for (int i = 1; i <= sceneCount; i++)
			{
				if (SceneIDs[i] == ScID)
				{
					result = i;
					break;
				}
			}
			return result;
		}

		public string DataAsXML()
		{
			string result = "";
			try
			{
				StringBuilder stringBuilder = new StringBuilder();
				string str = "\t\t";
				stringBuilder.AppendLine("\t<CHAPTER>");
				stringBuilder.Append(BaseDataAsXML());
				string value;
				if (mvarSectionStart)
				{
					value = str + cXml.EncloseBool(mvarSectionStart, "<SectionStart>", "</SectionStart>");
					stringBuilder.AppendLine(value);
				}
				value = str + cXml.EncloseInt((int)mvarType, "<Type>", "</Type>");
				stringBuilder.AppendLine(value);
				if (mvarBookID > 0)
				{
					value = str + cXml.EncloseInt(mvarBookID, "<BookID>", "</BookID>");
					stringBuilder.AppendLine(value);
				}
				if (mvarPartID > 0)
				{
					value = str + cXml.EncloseInt(mvarPartID, "<PartID>", "</PartID>");
					stringBuilder.AppendLine(value);
				}
				if (SceneIDs.Count() > 0)
				{
					value = str + "<Scenes>";
					stringBuilder.AppendLine(value);
					int num = SceneIDs.Count();
					for (int i = 1; i <= num; i++)
					{
						int nNumber = SceneIDs[i];
						value = str + "\t" + cXml.EncloseInt(nNumber, "<ScID>", "</ScID>");
						stringBuilder.AppendLine(value);
					}
					value = str + "</Scenes>";
					stringBuilder.AppendLine(value);
				}
				stringBuilder.AppendLine("\t</CHAPTER>");
				result = stringBuilder.ToString();
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
				return result;
			}
		}

		public bool CreateFromXML(string XMLData)
		{
			bool result = false;
			try
			{
				string[] DataArray = new string[2];
				Clear();
				if (!BaseCreateFromXML(XMLData))
				{
					return result;
				}
				mvarType = (ChapterTypes)cXml.ExtractInt(XMLData, "<Type>", "</Type>");
				mvarBookID = cXml.ExtractInt(XMLData, "<BookID>", "</BookID>");
				mvarPartID = cXml.ExtractInt(XMLData, "<PartID>", "</PartID>");
				mvarSectionStart = cXml.ExtractBool(XMLData, "<SectionStart>", "</SectionStart>");
				if (cXml.ExtractXMLArray(XMLData, "<SCENES>", "</SCENES>", "</SCID>", ref DataArray))
				{
					int num = DataArray.Length - 1;
					for (int i = 0; i <= num; i++)
					{
						if (DataArray[i].IndexOf("<SCID>", 0, StringComparison.OrdinalIgnoreCase) >= 0)
						{
							int num2 = cXml.ExtractInt(DataArray[i], "<SCID>", "</SCID>");
							SceneIDs.Add(num2, Conversions.ToString(num2));
						}
					}
				}
				result = true;
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				ProjectData.ClearProjectError();
				return result;
			}
		}

		public void RemoveAllScenes()
		{
			SceneIDs.Clear();
		}
	}
}
